home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / ODF / Examples / Draw / Sources / FWRuler.cpp < prev    next >
Encoding:
Text File  |  1995-11-08  |  6.8 KB  |  256 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWRuler.cpp
  4. //    Release Version:    $ 1.0d11 $
  5. //
  6. //    Copyright:    © 1995 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "ODFDraw.hpp"
  11.  
  12. #ifndef FWRULER_H
  13. #include "FWRuler.h"
  14. #endif
  15.  
  16. #ifndef FWFRAME_H
  17. #include "FWFrame.h"
  18. #endif
  19.  
  20. #ifndef FWRECSHP_H
  21. #include "FWRecShp.h"            // FW_CRectShape
  22. #endif
  23.  
  24. #ifndef FWTXTSHP_H
  25. #include <FWTxtShp.h>            // FW_CTextShape
  26. #endif
  27.  
  28. #ifndef FWLINSHP_H
  29. #include <FWLinShp.h>            // FW_CLineShape
  30. #endif
  31.  
  32. // ----- OpenDoc Includes -----
  33.  
  34. #ifndef SOM_ODFacet_xh
  35. #include <Facet.xh>
  36. #endif
  37.  
  38. #ifndef SOM_ODWindow_xh
  39. #include <Window.xh>
  40. #endif
  41.  
  42. #ifndef SOM_ODShape_xh
  43. #include <Shape.xh>
  44. #endif
  45.  
  46. //========================================================================================
  47. // Runtime Informations
  48. //========================================================================================
  49.  
  50. #if FW_LIB_EXPORT_PRAGMAS
  51. #pragma lib_export on
  52. #endif
  53.  
  54. #ifdef FW_BUILD_MAC
  55. #pragma segment fwgadgts
  56. #endif
  57.  
  58. FW_DEFINE_CLASS_M1(FW_CRuler, FW_CView)
  59.  
  60. //========================================================================================
  61. // CLASS FW_CRuler
  62. //========================================================================================
  63.  
  64. const FW_CFixed kRulerExtent = FW_IntToFixed(1000);
  65.  
  66. //----------------------------------------------------------------------------------------
  67. // FW_CRuler::FW_CRuler
  68. //----------------------------------------------------------------------------------------
  69.  
  70. FW_CRuler::FW_CRuler(Environment* ev, 
  71.                     FW_CView* container,
  72.                     const FW_CRect& bounds,
  73.                     int contentSpace) :
  74.     FW_CView(ev, container, bounds, 
  75.             (contentSpace & FW_CView::kXaxis) ? FW_CPoint(kRulerExtent, bounds.bottom - bounds.top) :
  76.                                                 FW_CPoint(bounds.right - bounds.left, kRulerExtent),
  77.             NULL,
  78.             contentSpace)
  79. {
  80.     FW_ASSERT(contentSpace != 0);
  81. }
  82.  
  83. //----------------------------------------------------------------------------------------
  84. // FW_CRuler::~FW_CRuler
  85. //----------------------------------------------------------------------------------------
  86.  
  87. FW_CRuler::~FW_CRuler()
  88. {
  89. }
  90.  
  91. //----------------------------------------------------------------------------------------
  92. // FW_CRuler::Draw
  93. //----------------------------------------------------------------------------------------
  94.  
  95. void FW_CRuler::Draw(Environment* ev, ODFacet* odFacet, ODShape* invalidShape)
  96. {
  97.     FW_CViewContext vc(ev, this, odFacet, invalidShape);
  98.     
  99.     FW_CRect bounds;
  100.     invalidShape->GetBoundingBox(ev, (ODRect*)&bounds);
  101.  
  102.     FW_CRectShape::RenderRect(vc, bounds, FW_kFill, FW_kWhiteEraseInk);
  103.  
  104.     if (UseContentSpaceInX(ev))
  105.         RenderHorizontalRuler(vc, bounds);
  106.     else
  107.         RenderVerticalRuler(vc, bounds);
  108. }
  109.  
  110. //----------------------------------------------------------------------------------------
  111. // FW_CRuler::RenderGradation
  112. //----------------------------------------------------------------------------------------
  113.  
  114. void FW_CRuler::RenderGradation(FW_CViewContext& vc,
  115.                                 short gradation, 
  116.                                 const FW_PFont& font, 
  117.                                 const FW_CPoint& pos)
  118. {
  119.     FW_CString32 string;
  120.  
  121. #ifdef FW_BUILD_MAC
  122.     Str255 pString;
  123.     NumToString(gradation, pString);    
  124.     string.ReplaceAll(pString);
  125. #endif
  126.  
  127. #ifdef FW_BUILD_WIN
  128.     FW_DEBUG_MESSAGE("Not Yet Implemented!");
  129. #endif
  130.  
  131.     FW_CTextShape::RenderText(vc, string,
  132.                             FW_CPoint(pos.x - FW_kFixedPos1, pos.y - FW_kFixedPos1),
  133.                             font,
  134.                             FW_kTextAlignRight | FW_kTextAlignBottom);
  135. }
  136.  
  137. //----------------------------------------------------------------------------------------
  138. // FW_CRuler::RenderHorizontalRuler
  139. //----------------------------------------------------------------------------------------
  140.  
  141. void FW_CRuler::RenderHorizontalRuler(FW_CViewContext& vc, const FW_CRect& visibleRect)
  142. {
  143.     FW_PStyle lineStyle(FW_kFixed0);
  144.     FW_PFont theFont(FW_kTimes, FW_kPlain, FW_IntToFixed(10));
  145.     
  146.     FW_CLineShape::RenderLine(vc, 
  147.                                 FW_CPoint(visibleRect.left, visibleRect.bottom - FW_kFixedPos1), 
  148.                                 FW_CPoint(visibleRect.right, visibleRect.bottom - FW_kFixedPos1), 
  149.                                 FW_kNormalInk, lineStyle);
  150.  
  151.     short h = visibleRect.Height().AsInt();
  152.     FW_CFixed full = FW_IntToFixed(2 * (h / 3));
  153.     FW_CFixed half = FW_IntToFixed(h / 2);
  154.     FW_CFixed third = FW_IntToFixed(h / 3);
  155.  
  156.     short gradation = visibleRect.left.AsInt() / 72;
  157.     FW_CFixed minX = FW_IntToFixed(72 * gradation);
  158.     FW_CFixed maxX = FW_IntToFixed(72 * ((visibleRect.right + FW_kFixed72).AsInt() / 72));
  159.     
  160.     FW_CFixed fixed18 = FW_IntToFixed(18);
  161.     
  162.     short n = 0;
  163.     FW_CFixed height = full;
  164.     if (minX == FW_kFixed0)
  165.     {
  166.         minX = fixed18;
  167.         n = 1;
  168.         height = third;
  169.     }
  170.     
  171.     FW_CPoint start(minX, visibleRect.bottom);
  172.     FW_CPoint end(minX, visibleRect.bottom - height);
  173.     for (FW_CFixed x = minX; x <= maxX; x += fixed18)
  174.     {
  175.         FW_CLineShape::RenderLine(vc, start, end, FW_kNormalInk, lineStyle);
  176.         if (n == 0)
  177.             RenderGradation(vc, gradation, theFont, start);
  178.         
  179.         n++;
  180.         if (n == 4)
  181.         {
  182.             n = 0;
  183.             gradation++;
  184.             height = full;
  185.         }
  186.         else if (n == 2)
  187.             height = half;
  188.         else if (n == 1 || n == 3)
  189.             height = third;
  190.         
  191.         start.x += fixed18;
  192.         end.x += fixed18;
  193.         end.y = visibleRect.bottom - height;
  194.     }
  195. }
  196.  
  197. //----------------------------------------------------------------------------------------
  198. // FW_CRuler::RenderVerticalRuler
  199. //----------------------------------------------------------------------------------------
  200.  
  201. void FW_CRuler::RenderVerticalRuler(FW_CViewContext& vc, const FW_CRect& visibleRect)
  202. {
  203.     FW_PStyle lineStyle(FW_kFixed0);
  204.     FW_PFont theFont(FW_kTimes, FW_kPlain, FW_IntToFixed(10));
  205.  
  206.     FW_CLineShape::RenderLine(vc, 
  207.                                 FW_CPoint(visibleRect.right - FW_kFixedPos1, visibleRect.top), 
  208.                                 FW_CPoint(visibleRect.right - FW_kFixedPos1, visibleRect.bottom), 
  209.                                 FW_kNormalInk, lineStyle);
  210.  
  211.     short h = visibleRect.Width().AsInt();
  212.     FW_CFixed full = FW_IntToFixed(2 * (h / 3));
  213.     FW_CFixed half = FW_IntToFixed(h / 2);
  214.     FW_CFixed third = FW_IntToFixed(h / 3);
  215.  
  216.     short gradation = visibleRect.top.AsInt() / 72;
  217.     FW_CFixed minY = FW_IntToFixed(72 * (visibleRect.top.AsInt() / 72));
  218.     FW_CFixed maxY = FW_IntToFixed(72 * ((visibleRect.bottom + FW_kFixed72).AsInt() / 72));
  219.     
  220.     FW_CFixed fixed18 = FW_IntToFixed(18);
  221.     
  222.     short n = 0;
  223.     FW_CFixed width = full;
  224.     if (minY == FW_kFixed0)
  225.     {
  226.         minY = fixed18;
  227.         n = 1;
  228.         width = third;
  229.     }
  230.         
  231.     FW_CPoint start(visibleRect.right, minY);
  232.     FW_CPoint end(visibleRect.right - width, minY);
  233.     for (FW_CFixed y = minY; y <= maxY; y += fixed18)
  234.     {
  235.         FW_CLineShape::RenderLine(vc, start, end, FW_kNormalInk, lineStyle);
  236.         if (n == 0)
  237.             RenderGradation(vc, gradation, theFont, start);
  238.         
  239.         n++;
  240.         if (n == 4)
  241.         {
  242.             n = 0;
  243.             gradation++;
  244.             width = full;
  245.         }
  246.         else if (n == 2)
  247.             width = half;
  248.         else if (n == 1 || n == 3)
  249.             width = third;
  250.         
  251.         start.y += fixed18;
  252.         end.y += fixed18;
  253.         end.x = visibleRect.right - width;
  254.     }
  255. }
  256.